package persistencia;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import modelo.Cliente;
import modelo.OS;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import org.xmlpull.v1.XmlPullParserException;

public class DALOS {

	public OS LerOS(String CodOS) throws IOException, XmlPullParserException {
		
		List<PropertyInfo> lsPI = new ArrayList<PropertyInfo>();
		
		PropertyInfo pi = new PropertyInfo();
		pi.setName("strCodOS");
		pi.setValue(CodOS);
		
		lsPI.add(pi);
				
		WebServices ws = new WebServices();
		SoapObject result = ws.WebServices("LerDadosOS",lsPI);
				
		OS oOs = new OS();
		
		if (result != null){
				
			oOs.setIdOS(Integer.parseInt(result.getProperty("idOS").toString()));
			oOs.setCod(result.getProperty("Cod").toString());
			oOs.setIdCliente(Integer.parseInt(result.getProperty("idCliente").toString()));
			
			Cliente oCl = new Cliente();
			oCl = new DALCliente().LerCliente(oOs.getIdCliente());
			oOs.setoCliente(oCl);
			
		}else 
			oOs = null;
				
		return oOs;
			
	}

	public List<OS> ListarOS() throws IOException, XmlPullParserException{
		
		WebServices ws = new WebServices();
		SoapObject result = ws.WebServices("ListarOSs",null);
		
		List<OS> oOSs = new ArrayList<OS>();
		
		for (int i = 0; i < result.getPropertyCount(); i++) {

			SoapObject parqueSoap = (SoapObject) result.getProperty(i);
			
			OS oOs = new OS();
			oOs.setIdOS(Integer.parseInt(parqueSoap.getProperty("idOS").toString()));
			oOs.setCod(parqueSoap.getProperty("Cod").toString());
			oOs.setIdCliente(Integer.parseInt(parqueSoap.getProperty("idCliente").toString()));
			
			Cliente oCl = new Cliente();
			oCl = new DALCliente().LerCliente(oOs.getIdCliente());
			oOs.setoCliente(oCl);
			
			oOSs.add(oOs);
			
		}
		
		return oOSs;
	}
	
}